import { get, post, put, del } from '../utils/request';
import { LoginParams, UserInfo } from '../utils/auth';

// API响应的通用接口
interface ApiResponse<T> {
  code: number;
  data: T;
  message: string;
}

// 分页数据接口
export interface PaginationData<T> {
  list: T[];
  total: number;
  current: number;
  pageSize: number;
}

// 查询参数接口
export interface QueryParams {
  page?: number;
  pageSize?: number;
  [key: string]: any;
}

/**
 * 认证相关API
 */
export const AuthApi = {
  // 登录
  login: (params: LoginParams) => post<ApiResponse<{ token: string; user: UserInfo }>>('/auth/login', params),
  
  // 注销
  logout: () => post<ApiResponse<null>>('/auth/logout'),
  
  // 获取当前用户信息
  getCurrentUser: () => get<ApiResponse<UserInfo>>('/auth/current-user'),
  
  // 修改密码
  changePassword: (data: { currentPassword: string; newPassword: string }) => 
    put<ApiResponse<null>>('/auth/change-password', data),
};

/**
 * 用户管理API
 */
export const UserApi = {
  // 获取用户列表
  getUsers: (params: QueryParams) => get<ApiResponse<PaginationData<UserInfo>>>('/users', params),
  
  // 获取用户详情
  getUser: (id: string) => get<ApiResponse<UserInfo>>(`/users/${id}`),
  
  // 创建用户
  createUser: (data: Partial<UserInfo>) => post<ApiResponse<UserInfo>>('/users', data),
  
  // 更新用户
  updateUser: (id: string, data: Partial<UserInfo>) => put<ApiResponse<UserInfo>>(`/users/${id}`, data),
  
  // 删除用户
  deleteUser: (id: string) => del<ApiResponse<null>>(`/users/${id}`),
};

/**
 * 仪表盘API
 */
export const DashboardApi = {
  // 获取统计数据
  getStats: () => get<ApiResponse<{
    userCount: number;
    orderCount: number;
    totalRevenue: number;
    fileCount: number;
  }>>('/dashboard/stats'),
  
  // 获取最近订单
  getRecentOrders: () => get<ApiResponse<Array<{
    id: number;
    customer: string;
    amount: number;
    status: string;
    date: string;
  }>>>('/dashboard/recent-orders'),
  
  // 获取通知
  getNotifications: () => get<ApiResponse<Array<{
    id: number;
    title: string;
    description: string;
    time: string;
  }>>>('/dashboard/notifications'),
};

/**
 * 设置API
 */
export const SettingsApi = {
  // 获取通用设置
  getGeneralSettings: () => get<ApiResponse<{
    siteName: string;
    siteDescription: string;
    adminEmail: string;
    itemsPerPage: number;
    enableNotifications: boolean;
    language: string;
    theme: string;
  }>>('/settings/general'),
  
  // 更新通用设置
  updateGeneralSettings: (data: any) => put<ApiResponse<null>>('/settings/general', data),
  
  // 获取安全设置
  getSecuritySettings: () => get<ApiResponse<{
    enableTwoFactor: boolean;
    sessionTimeout: number;
    passwordExpiry: number;
    minimumPasswordLength: number;
    requireSpecialChars: boolean;
  }>>('/settings/security'),
  
  // 更新安全设置
  updateSecuritySettings: (data: any) => put<ApiResponse<null>>('/settings/security', data),
};

/**
 * 个人信息API
 */
export const ProfileApi = {
  // 获取个人信息
  getProfile: () => get<ApiResponse<UserInfo>>('/profile'),
  
  // 更新个人信息
  updateProfile: (data: Partial<UserInfo>) => put<ApiResponse<UserInfo>>('/profile', data),
  
  // 上传头像
  uploadAvatar: (formData: FormData) => post<ApiResponse<{ avatarUrl: string }>>('/profile/avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  }),
};